package dtaskboard.client.login;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

import dtaskboard.client.Constants;
import dtaskboard.client.Dtaskboard;
import dtaskboard.client.service.LoginService;
import dtaskboard.client.service.LoginServiceAsync;
import dtaskboard.client.util.EnterListener;

public class LoginPage {

  private final TextBox login;
  private final TextBox password;
  private final Label message;

  public LoginPage() {

    RootPanel.get(Constants.mainPanel).clear();
    RootPanel.get(Constants.toolbarPanel).clear();
    RootPanel.get(Constants.projectToolbar).clear();

    this.message = new Label();

    FlexTable loginTable = new FlexTable();
    loginTable.addStyleName("loginTable");

    LoginConstants i18n = GWT.create(LoginConstants.class);

    Label loginLabel = new Label(i18n.login());
    loginLabel.setStyleName("loginLabel");
    this.login = new TextBox();
    loginTable.setWidget(0, 0, loginLabel);
    loginTable.setWidget(0, 1, this.login);

    Label passLabel = new Label(i18n.password());
    passLabel.setStyleName("loginLabel");
    this.password = new PasswordTextBox();
    loginTable.setWidget(1, 0, passLabel);
    loginTable.setWidget(1, 1, this.password);

    Button submit = new Button();
    submit.setText(i18n.submit());
    submit.addClickHandler(new LoginSubmitHandler(this.message));
    loginTable.setWidget(2, 0, submit);
    loginTable.getFlexCellFormatter().setColSpan(2, 0, 2);
    loginTable.getFlexCellFormatter().setStyleName(2, 0, "loginSubmit");

    loginTable.setWidget(3, 0, this.message);
    loginTable.getFlexCellFormatter().setColSpan(3, 0, 2);

    KeyDownHandler submitter = new EnterListener(submit);
    this.login.addKeyDownHandler(submitter);
    this.password.addKeyDownHandler(submitter);

    Panel mainPanel = RootPanel.get(Constants.mainPanel);
    mainPanel.clear();
    mainPanel.addStyleName("loginTable");
    mainPanel.add(loginTable);
  }

  class LoginSubmitHandler implements ClickHandler {
    private final LoginServiceAsync loginService = GWT
        .create(LoginService.class);
    private final LoginConstants i18n = GWT.create(LoginConstants.class);

    private final Label message;

    public LoginSubmitHandler(Label message) {
      this.message = message;
    }

    @Override
    public void onClick(ClickEvent event) {
      this.loginService.login(LoginPage.this.login.getText(),
          LoginPage.this.password.getText(), new AsyncCallback<String>() {

            @Override
            public void onSuccess(String sessionId) {
              Cookies.setCookie(Constants.sessionIdCookie, sessionId);
              Dtaskboard reload = new Dtaskboard();
              reload.onModuleLoad();
            }

            @Override
            public void onFailure(Throwable caught) {
              LoginSubmitHandler.this.message
                  .setText(LoginSubmitHandler.this.i18n.loginFailed());
            }
          });
    }
  }
}
